<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :rules="rules"
        :model="loginForm"
        class="login_form"
        label-width="0px"
      >
        <!-- 用户名 -->
        <el-form-item label="" prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="fa fa-address-book"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="fa fa-lock"
          ></el-input>
        </el-form-item>
        <!-- 提交按钮 -->
        <el-form-item label="" class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //这是表单的数据绑定对象
      loginForm: {
        username: "admin",
        password: "123456",
      },
      //表单验证规则
      rules: {
        //验证用户名合法性
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,
            max: 16,
            message: "密码长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //重置表单功能
    resetLoginForm: function () {
      //通过this的$refs 属性里面的 resetFields函数重置表单
      this.$refs.loginFormRef.resetFields();
    },
    //登录功能
    async login() {
      // 登录验证功能  validate返回一个promise对象，可以用await语法,也可以用回调函数方法
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) {
          return;
        } else {
          //这里是通过验证规则的情况，发送请求
          const { data: res } = await this.$http.post(`login`, this.loginForm);
          if (res.meta.status == 200) {
            //登录成功
            console.log(res);
            this.$message.success({
              message: "登录成功",
              showClose: true,
            });
            //登录成功后 将token保存到sessionStorage
            sessionStorage.setItem("token", res.data.token);
            //利用this.$router.push() 进行前端路由跳转
            this.$router.push("/home");
          } else {
            //登录失败
            console.log(res);
            this.$message({
              showClose: true,
              message: "请检查用户名或密码",
              type: "error",
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  .login_box {
    // overflow: hidden;
    position: absolute;
    background-color: #fff;
    width: 450px;
    height: 300px;
    border-radius: 15px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      // z-index: 5;
      width: 130px;
      height: 130px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      border: #eee 1px solid;
      padding: 10px;
      top: 0%;
      left: 50%;
      transform: translate(-50%, -50%);
      border-radius: 50%;
      background-color: #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .login_form {
      padding: 20px;
      width: 100%;
      box-sizing: border-box;
      position: absolute;
      bottom: 0;
      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
